package com.itianshan.wheel;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bluetooth.SearchBluetoothActivity;
import com.usbserial.SearchUsbActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        int width = getScreenWidth();
        FontIconView bluetooth = findViewById(R.id.bluetooth);
        bluetooth.setTextSize(width/2);
        FontIconView usb = findViewById(R.id.usb);
        usb.setTextSize(width/2);
        FontIconView cloud_car = findViewById(R.id.cloud_car);
        cloud_car.setTextSize(width/2);
        FontIconView cloud_wheel = findViewById(R.id.cloud_wheel);
        cloud_wheel.setTextSize(width/2);

        bluetooth.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                ComponentName bluetooth = new ComponentName(MainActivity.this, SearchBluetoothActivity.class);
                Intent intent = new Intent();
                intent.setComponent(bluetooth);
                startActivity(intent);
            }
        });
        usb.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                ComponentName usb = new ComponentName(MainActivity.this, SearchUsbActivity.class);
                Intent intent = new Intent();
                intent.setComponent(usb);
                startActivity(intent);
            }
        });
        cloud_car.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle(R.string.select_cloud_car)
                        //.setMessage("您确实要删除吗？")
                        .setNegativeButton(R.string.bluetooth_devices, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ComponentName component = new ComponentName(MainActivity.this, SearchBluetoothActivity.class);
                                Intent intent = new Intent();
                                intent.putExtra("is_cloud", true);
                                intent.setComponent(component);
                                startActivity(intent);
                            }
                        })//负面按钮

                        .setPositiveButton(R.string.usb_devices, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ComponentName component = new ComponentName(MainActivity.this, SearchUsbActivity.class);
                                Intent intent = new Intent();
                                intent.putExtra("is_cloud", true);
                                intent.setComponent(component);
                                startActivity(intent);
                            }
                        })//正面按钮
                        /*
                        .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(Main3Activity.this, "执行命令……，which = " + which, Toast.LENGTH_SHORT).show();
                            }
                        })//普通按钮
                         */
                        .setCancelable(true) //点击其他位置关闭对话框 不能以分号结尾
                        .show(); //显示对话框的方法
            }
        });
        cloud_wheel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                ComponentName cloud_wheel = new ComponentName(MainActivity.this, CloudWheelEntranceActivity.class);
                Intent intent = new Intent();
                intent.putExtra("device_type","cloud_wheel");
                intent.setComponent(cloud_wheel);
                startActivity(intent);
            }
        });
    }
    private int getScreenWidth(){
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getRealMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        float density = dm.density;
        int densityDpi = dm.densityDpi;
        int screenWidth = (int) (width/density);
        int screenHeight = (int) (height/density);
        return screenWidth;
    }
}
